//
//  HBFollowItemController.m
//  HeartBeat
//
//  Created by karong on 2023/8/28.
//

#import "HBFollowItemController.h"
#import "HBFollowItemCell.h"
#import "HBFollowRequest.h"
#import "HBFollowEmptyCell.h"
@interface HBFollowItemController ()  <HBFollowEmptyCellDlegate>
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *editBarBottom;
@property (weak, nonatomic) IBOutlet UIButton *deleteBtn;
@property (nonatomic, assign) BOOL isEdit;
@property (nonatomic, assign) BOOL needReload;
@end

@implementation HBFollowItemController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.needReload) {
        self.needReload = NO;
        [self benginRefresh];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addObserver];
    HBFollowRequest * request = [HBFollowRequest new];
    self.request = request;
    
}

- (void)addObserver {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(needReloadData) name:HBRefreshFollowNotification object:nil];
    
}

- (void)needReloadData {
    if ([[UIViewController currentViewController] isKindOfClass:NSClassFromString(@"HBFollowController")]) {
        [self benginRefresh];
    }
    else {
        self.needReload = YES;
    }
}

- (BOOL)disableAutoBeginRefresh {
    return appUserCenter.isLogin;
}

- (void)beginEdit {
    self.isEdit = YES;
}

- (void)endEdit {
    self.isEdit = NO;
}

- (NSInteger)dataArrayCount {
    return self.dataArray.count;
}

- (IBAction)cancelAction:(UIButton *)sender {
    [self endEdit];
}

- (IBAction)deleteAction:(id)sender {
    NSMutableArray * delArray = [NSMutableArray new];
    for (HBNovelModel * novel in self.dataArray) {
        if (novel.isSelected) {
            [delArray addObject:novel];
        }
    }
    if (delArray.count > 0) {
        [self delFollowRequest:delArray];
    }
    else {
        [self endEdit];
    }
}

- (void)delFollowRequest:(NSArray*)array {
    [HBToast loading];
    [HBRequest delBookshelfWithDelArray:array success:^{
        [HBToast dismiss];
        [self.dataArray removeObjectsInArray:array];
        [self endEdit];
//        [self benginRefresh];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (void)initArraySelectStatus {
    for (HBNovelModel * novel in self.dataArray) {
        novel.isSelected = NO;
    }
}

- (void)layoutDeleteBtnStatus {
    BOOL rec = NO;
    for (HBNovelModel * novel in self.dataArray) {
        if (novel.isSelected) {
            rec = YES;
            break;
        }
    }
    
    self.deleteBtn.backgroundColor = rec?UIColor.font_yellow_FFCC00:[UIColor colorFromHexRGB:@"e5e5e5"];
    [self.deleteBtn setTitleColor:rec?UIColor.font_black_1A1A1A:UIColor.font_white_ffffff forState:(UIControlStateNormal)];
}
#pragma mark CollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return MAX(self.dataArray.count, 1);
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataArray.count > 0) {
        HBFollowItemCell * followItemCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBFollowItemCell" forIndexPath:indexPath];
        [followItemCell setDataWithObject:self.dataArray[indexPath.row] edit:self.isEdit];
        return followItemCell;
    }
    else {
        HBFollowEmptyCell * followEmptyCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBFollowEmptyCell" forIndexPath:indexPath];
        followEmptyCell.delegate = self;
        return followEmptyCell;
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataArray.count > 0) {
        HBNovelModel * novel = self.dataArray[indexPath.row];
        if (self.isEdit) {
            novel.isSelected = !novel.isSelected;
            [self layoutDeleteBtnStatus];
            [self.listView reloadData];
        }
        else {
                ///这里跳转阅读详情是因为未读的书籍，chapterid下发的是0
                [ViewManager pushNovelDetailVCWithNovel:novel inController:self];
                [HBEvenManager event:@"book_tracking" evenLogType:(HBEvenLogType_Click)
                          attributes:@{@"page_view":@"1"}];
        }
    }
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    CGFloat width = 70;
    size.width = HBScreenWidth;
    
    if (self.dataArray.count > 0) {
        size.height = width * 4 / 3.0f;
    }
    else {
        size.height = collectionView.height;
    }
    return size;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 12;
}

#pragma mark HBFollowEmptyCellDlegate
- (void)followEmptyCellAction {
    [ViewManager pushRankVCInController:self];
}

- (void)setIsEdit:(BOOL)isEdit {
    _isEdit = isEdit;
    self.editBarBottom.constant = isEdit?0:-80;
    [self.view uploadLayout];
    if (isEdit) {
        [self initArraySelectStatus];
        [self layoutDeleteBtnStatus];
    }
    else {
        if ([self.delegate respondsToSelector:@selector(followItemControllerExitEdit)]) {
            [self.delegate followItemControllerExitEdit];
        }
    }
    [self.listView reloadData];
}
@end
